package com.sq.partner.manager.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.dao.ActirationCardCancelDao;
import com.sq.partner.manager.mybatis.model.ActirationCardCancel;
import com.sq.partner.manager.service.ActirationCardCancelService;
import com.sq.partner.manager.util.Page;

@Service
public class ActirationCardCancelServiceImpl implements
        ActirationCardCancelService {

    @Resource
    private ActirationCardCancelDao cardCancelDao;

    @Override
    public Page<ActirationCardCancel> queryStock(Page<ActirationCardCancel> page)
            throws Exception {
        return cardCancelDao.queryPage(page);
    }

    /**
     * 作废操作 根据list
     * 
     * @param cardDefineIdList
     * @param cardNoList
     * @return
     * @throws Exception
     */
    @Override
    public Result cardCancelList(List<String> cardDefineIdList,
            List<String> cardNoList) throws Exception {
        HashMap<String, Object> parmMap = null;
        Boolean flag = true;
        boolean first = false;
        StringBuffer bf = null;
        for (int i = 0; i < cardDefineIdList.size(); i++) {
            parmMap = new HashMap<String, Object>();
            parmMap.put("cardDefineId", cardDefineIdList.get(i));
            parmMap.put("cardNo", cardNoList.get(i));
            parmMap.put("status", Constants.ACTIR_CARD_WASTE);
            parmMap.put("statusBef", Constants.ACTIR_CARD_STOCK);

            flag = cardCancelDao.updateCardCancel(parmMap);
            if (!flag) {
                if (!first) {
                    first = true;
                    bf = new StringBuffer("以下卡单状态变更，作废失败！卡号：");
                    bf.append(cardNoList.get(i));
                } else {
                    bf.append(",");
                    bf.append(cardNoList.get(i));
                }
            }
        }
        if (bf == null) {
            return Result.success("作废成功！");
        } else {
            return Result.success(bf.toString());
        }
    }

    /**
     * 作废操作，根据起止号
     * 
     * @param startNo
     * @param endNo
     * @param date
     * @return
     * @throws Exception
     */
    @Override
    public Result cardCancelBatch(HashMap<String, Object> paramMap)
            throws Exception {
        paramMap.put("status", Constants.ACTIR_CARD_WASTE);
        paramMap.put("statusBef", Constants.ACTIR_CARD_STOCK);
        boolean flag = cardCancelDao.updateCardCancelBatch(paramMap);
        if (!flag) {
            return Result.error("作废失败，请重新查询！");
        } else {
            return Result.success("作废成功！");
        }
    }

    /**
     * 作废统计查询
     */
    @Override
    public Page<ActirationCardCancel> queryCancel(
            Page<ActirationCardCancel> page) throws Exception {
        return cardCancelDao.queryCancelPage(page);
    }

    @Override
    public Page<ActirationCardCancel> queryCancelDetail(
            Page<ActirationCardCancel> page) throws Exception {
        return cardCancelDao.queryCancelDetailPage(page);
    }

}
